# -*- coding: utf-8 -*-
"""
    Created by 杜神 at 2018/3/21
"""
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
from spider.items import H6080Item, H6080MovieInfo
from scrapy.contrib.spiders import CrawlSpider, Rule
from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor
import logging
import re


class SpiderNo1(CrawlSpider):
    name = 'h6080spider'
    allowed_domains = ['h6080.com']
    start_urls = [
        'http://www.h6080.com/show/2118.html',
    ]
    logging.getLogger("requests").setLevel(logging.WARNING
                                           )  # 将requests的日志级别设成WARNING
    logging.basicConfig(
        level=logging.INFO,
        format=
        '%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
        datefmt='%a, %d %b %Y %H:%M:%S',
        filename='h6080spider.log',
        filemode='w')

    rules = (
        # follow所有分类链接/下一页链接/详情页链接
        Rule(SgmlLinkExtractor(allow=('\/type\/.*\.html', )), follow=True),
        Rule(SgmlLinkExtractor(allow=('\/play\/.*\.html', )), callback='parse_item', follow=False),
        Rule(SgmlLinkExtractor(allow=('\/show\/.*\.html', )), callback='parse_movie_info', follow=True),
    )

    def parse_item(self, response):
        item = H6080Item()
        item['name'] = response.xpath('//h3[@class="movie-title"]/text()').extract()[0]
        item['url'] = response.xpath('//div[@class="player"]/iframe/@src').extract()[0]
        item['num'] = re.findall('\/\d+\.html', response.url)[0].split('.')[0][1:]
        self.log('find a url! %s' % response.url)
        yield item

    def parse_movie_info(self, response):
        item = H6080MovieInfo()
        rows = response.xpath('//td/text()').extract()
        item['director'] = rows[0]
        item['actor'] = rows[1]
        item['types'] = rows[2]
        item['area'] = rows[3]
        try:
            item['publishtime'] = rows[5]
        except Exception:
            item['publishtime'] = '1999-01-01'
        item['name'] = response.xpath('//h1[@class="movie-title"]/text()').extract()[0]
        item['introduce'] = response.xpath('//p[@class="summary"]/text()').extract()[0]
        item['picurl'] = response.xpath('//img[@class="img-thumbnail"]/@src').extract()[0]
        if len(re.findall('\d+', rows[4])) > 0:
            item['countnumber'] = re.findall('\d+', rows[4])[0]
        else:
            item['countnumber'] = 0
        self.log('find a movie! %s' % item['name'])
        yield item
